//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends egret.DisplayObjectContainer 
{
    private loadingView: LoadingUI;
    private scene: GameSceneView;
    private swfFrame: any;
    private loadTimes: number = 0;



    public constructor()
    {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

        RES.registerAnalyzer("starlingswf_sheet", starlingswf.StarlingSwfSheetAnalyzer);
    }

    private onAddToStage(evt: egret.Event): void
    {
        let title = "redhat_mini";
        let content = "xxxxxxxxxxxxxcontent";
        let link = "http://static.egret-labs.org/h5game/62/v20/index.html";
        let ico = "http://static.egret-labs.org/h5game/icons/10000062.jpg";

        Const.SENT_WIDTH = this.stage.stageWidth;
        Const.SENT_HEIGHT = this.stage.stageHeight;
        console.log("xxxxxxxxxxxxxxxxhei"+Const.SENT_HEIGHT);
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/resource.json", "resource/");
        console.log("main.onAddToStage");
    }

    private onConfigComplete(evt: RES.ResourceEvent): void
    {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProcess, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onGroupErr, this);
        RES.createGroup("initLoad", ["preload", "bgPic", "animation", "sound"]);
        RES.loadGroup("initLoad");
    }

    private onResourceLoadComplete(evt: RES.ResourceEvent): void
    {
        if (evt.groupName == "initLoad")
        {
            this.loadingView.onLoadComplete(this.onStartGame, this);
        }

    }

    private onResourceProcess(evt: RES.ResourceEvent): void
    {
        if (evt.groupName == "initLoad")
        {
            this.loadingView.setProgress(evt.itemsLoaded, evt.itemsTotal);
        }
    }

    private onGroupErr(evt: RES.ResourceEvent): void
    {

    }

    private onStartGame(): void
    {
        this.stage.removeChild(this.loadingView);
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProcess, this);
        this.initAnimationData();
        this.createGameScene();
    }

    private initAnimationData(): void
    {
        let arr: Array<string> = ["redNu",
        "promptPop",
        "lang",
        "go",
        "xiaohongmao",
        "daoju",
        "lieren",
        "huli",
        "bianfu",
        "niao"
        ];

        for (let i: number = 0, len:number = arr.length; i < len; ++i)
        {
            let key: string = arr[i];
            //加载swf动画
            StarlingSwfFactory.getInstance().addSwf(key, RES.getRes(key + "_swf_json"), RES.getRes(key + "_json"));
        }
    }

    private createGameScene(): void
    {
        Const.GamePoxY = 0;
        GameData.curScene = 1;

        var maskRect: egret.Rectangle = new egret.Rectangle();
        maskRect.width = Const.SENT_WIDTH;
        maskRect.height = Const.SENT_HEIGHT;
        maskRect.y = Const.GamePoxY;

        //加载音频文件
        SoundUtils.instance().initSound();
        let scene = new GameSceneView();
        scene.y = Const.GamePoxY;
        scene.width = Const.SENT_WIDTH;
        scene.height = Const.SENT_HEIGHT;
        scene.mask = maskRect;
        this.addChild(scene);
        this.scene = scene;
    }

    
}